package com.poker.service;

import com.poker.entity.Activity;
import com.poker.entity.ActivityContent;
import com.poker.entity.ManagerInfo;
import com.poker.repository.ActivityContentRepository;
import com.poker.repository.ActivityRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ActivityService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 11:04
 * @Version 1.0
 **/
@Service
public class ActivityService {

    @Resource
    private ActivityRepository activityRepository;

    @Resource
    private ActivityContentRepository contentRepository;

    public Page<Activity> findList(Integer page, Integer size,Integer classifya) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        if(classifya!=null){
            Specification<Activity> spec = new Specification<Activity>() {        //查询条件构造
                public Predicate toPredicate(Root<Activity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Path<Integer> classify = root.get("classify");
                    Predicate p1 = cb.equal(classify,classifya);
                    Predicate p = cb.and(p1);
                    return p;
                }
            };

            return activityRepository.findAll(spec,pageable);
        }
        return activityRepository.findAll(pageable);
    }
    public Page<Activity> findListAll(Integer page, Integer size, Activity activity, Integer classify) {

        Pageable pageable = PageRequest.of(page, size,Sort.Direction.DESC, "createTime");
        Specification<Activity> spec = new Specification<Activity>() {        //查询条件构造
        public Predicate toPredicate(Root<Activity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Path<Integer> classify = root.get("classify");
            Path<String> activityName = root.get("activityName");
            Predicate p1 = cb.like(activityName, "%"+activity.getActivityName()+"%");
            Predicate p2 = cb.equal(classify,classify);
         Predicate p = cb.and(p1,p2);
         return p;
           }
          };
          return   activityRepository.findAll(spec,pageable);
    }

    public Activity getById(String id) {
        Activity one = activityRepository.getOne(id);
         if(one!=null){
             ActivityContent content = findByActivityId(one);
             if(content!=null){
                 one.setContent(content.getContent());
             }
         }
        return one;
    }

    public void update(Activity activity) {
            Activity one = activityRepository.getOne(activity.getId());
            activity.setCreateTime(one.getCreateTime());
            activityRepository.saveAndFlush(activity);
            ActivityContent content = findByActivityId(one);
            if(content!=null) {
                content.setContent(activity.getContent());
                contentRepository.saveAndFlush(content);
            }else {
                ActivityContent contents = new ActivityContent();
                contents.setActivityId(activity.getId());
                contents.setContent(activity.getContent());
                contentRepository.saveAndFlush(contents);
            }

    }

    public ActivityContent findByActivityId(Activity one){
        ActivityContent content = contentRepository.findByActivityId(one.getId());
        return content;
    }

    public void saveActivityAndContent(Activity activity) {
        ActivityContent contents = new ActivityContent();
        activity.setCreateTime(new Date());
        activityRepository.saveAndFlush(activity);
        contents.setContent(activity.getContent());
        contents.setActivityId(activity.getId());
        contentRepository.saveAndFlush(contents);
    }

    public void deleteActivityAndContent(Activity activity) {
        ActivityContent content = contentRepository.findByActivityId(activity.getId());
        activityRepository.delete(activity);
        contentRepository.delete(content);

    }

    public List<Activity> findActIng() {
        return activityRepository.findActIng();
    }

    public List<Activity> findActEnd() {
        return activityRepository.findActEnd();
    }

    public List<Activity> findActSign() {
        return activityRepository.findActSign();
    }

    public void updateStatus(Activity activity) {
        activityRepository.saveAndFlush(activity);
    }


}
